FoxPro and AppleScript
Volume Number: 10
Issue Number: 7
Column Tag: Applescript
Related Info: Apple Event Mgr
FoxPro and AppleScript
Drive other mac applications to create your own workflow solution
By Randy Brown, Sierra Systems
About the author
Randy is a consultant for Sierra Systems in Northern California. He specializes
in custom FoxPro development on Macintosh, Windows and DOS. Prior to founding
Sierra Systems, Randy consulted with Ernst & Young’s National Energy Practice in San
Francisco.
Randy coauthored FoxPro MAChete: Hacking FoxPro for Macintosh, by Hayden
Books, from which this article is excerpted. Last year, Randy completed his second
book, FoxPro 2.5 OLE and DDE, a book by Pinnacle Publishing. Randy has written
numerous articles on a variety of FoxPro technical topics for magazines like Dr. Dobbs
Journal and FoxTalk. Randy is a speaker on the FoxPro conference circuit, which
includes the Microsoft FoxPro Devcon and the Minnesota FoxPro User’s Conference.
You can reach him at:
Internet: 71141.3014@compuserve.com
CompuServe: 71141,3014
America OnLine: RandyBrwn
Wouldn’t it be nice if you could have Microsoft FoxPro for Macintosh output a
database query and have Microsoft Excel automatically create a chart from that data?
Wouldn’t it be nice if this chart, along with an associated data table, could be inserted
into an executive report in your favorite word processor such as WordPerfect?
Wouldn’t it be nice if you could print this report, fax and/or E-Mail it to any
destination of your choosing?
This article explores integrating Microsoft FoxPro/Mac with other applications
using AppleScript™. If you are new to FoxPro and/or AppleScript, this article can
help you gain a better understanding of these concepts. There are additional reference
materials for mastering the elements of FoxPro and AppleScript.
Before we jump into lots of juicy code, I should warn you that running
AppleScript with FoxPro is going to chew up lots of RAM. In fact, I would recommend a
minimum of 8MB since FoxPro itself likes to have at least 5MB. And depending on the
other application(s), you may need more.
In short, FoxPro for Macintosh supports the Required Suite and one critical
additional command which it includes in a Miscellaneous Suite. What does this mean?
Well, other applications can only make calls to FoxPro with these commands:
Required Suite:
Open - opens the specified object(s).
Print - print the specified object(s).
Quit - quit application.
Run - sent to application when it is double-clicked.
Miscellaneous Suite:
Do Script - execute a script.
I guess we can stop here, since there is not a whole lot of AppleScript stuff which
FoxPro supports, right? Not quite. Using AppleScript is a two-sided equation, so
learning about FoxPro’s support of AppleScript is only half the work. Examine
Microsoft Excel for a minute. When I write a script (a script is the document
containing the AppleScript code - similar to a FoxPro .PRG), that script can contain
any commands and objects supported by the application. A script communicating with
Excel can contain many AppleScript commands since Excel supports a fairly large set.
In fact, Excel supports 5 separate suites of Apple Events (Required, Core, Table,
Charting and Excel). So even though talking to FoxPro often involves small scripts,
going in the other direction can be a different story.
Scripting to FoxPro
As you may know, FoxPro has a command, RUNSCRIPT, which is used to run a
script from within a FoxPro program. I’ll go into this later, but you may not know
that other applications can talk to FoxPro. Here is a very simple script you can try. If
FoxPro is not running, the script will automatically launch it for you.
/* 1 */
tell application "Microsoft FoxPro
DO SCRIPT "WAIT WINDOW
'This is my first script' TIMEOUT 2
end tell
This simple script uses the DO SCRIPT command, the only non-Required suite
AppleScript command FoxPro supports. The DO SCRIPT command lets you run any
FoxPro command. While the FoxPro WAIT WINDOW command is just one of many
possibilities, you would most likely pass a DO command to run a program (PRG or
APP) to execute a sequence of commands. You wouldn’t want to string a bunch of
FoxPro commands using separate DO SCRIPT AppleScript commands, because going
back and forth between FoxPro and AppleScript is slow. These hits may not be all that
significant, but you should try to avoid a lot of switching between applications.
This next script shows some of the commands which FoxPro supports from the
Required Events suite (Run, Open, Print, Quit). The use of FoxPro’s INKEY() function
here illustrates a neat trick you can use to make FoxPro pause for a specified time.
/* 2 */
- Sample AppleScript showing Required commands.
run application "Microsoft FoxPro
tell application "Microsoft FoxPro
activate "Microsoft FoxPro
open "config.fpm
Do Script "=INKEY(2)
activate "Finder
end tell
One of the more intriguing AppleScript ideas is accessing FoxPro as a server.
The concept of using FoxPro in a client-server capacity based on AppleScript is pretty
cool. Methods of requesting data can come in multiple flavors. For instance, you can
write a script to request data by dropping a special request file on an AppleScript
droplet. If you want to get a little fancier, take a look at a neat shareware utility called
Folder Watcher (by Joe Zobkiw). Folder Watcher is actually a control panel/ system
extension combo which monitors the contents of any specified folder(s). Through the
control panel, you can control a number of actions taken when the contents of a watched
folder changes. Typically you have your machine display a dialog and/or play a sound.
A more powerful option, however, is to have an AppleScript run when a folder’s
contents change. Think of the possibilities. For example, you could set up a folder
called “Requests” into which you drop files which FoxPro could then automatically
process through a script.
Scripting from FoxPro
Here is the only command FoxPro has devoted to AppleScript. Unlike XCMDs,
values passed back from scripts are not limited to strings. Scripts, for example, can
also return numeric data types.
/* 3 */
RUNSCRIPT